//
//  LSZNetworkTools.swift
//  醒浪微博
//
//  Created by 雷顺哲 on 16/7/10.
//  Copyright © 2016年 LSZ. All rights reserved.
//

import UIKit
import AFNetworking
//定义枚举,默认从0开始
enum RequestType: Int {
    case GET
    case POST
}

//网路请求工具类
class LSZNetworkTools: AFHTTPSessionManager {
    
    //  闭包回调别名
    typealias CallBack = (response: AnyObject?, error: NSError?)->()
//    单例类
    static let sharedToos: LSZNetworkTools = {
        let toos = LSZNetworkTools()
//        添加解码格式
        toos.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return toos
    }()
    
    
    func requestUpload(url: String, params: AnyObject?, imageData: NSData, name: String, callBack: CallBack){
        //        上传图片的接口
        POST(url, parameters: params, constructingBodyWithBlock: { (formData) -> Void in
            
            /**
            imageData : 上传图片所需要二进制数据
            name: 服务端的获取数据的参数
            fileName: 告诉服务端我们的文件名字,服务端一般不会用,他们会自动生成唯一的一个图片名
            mimeType: 资源类型, octet-stream通用的资源类型
            */
            formData.appendPartWithFileData(imageData, name: name, fileName: "test", mimeType: "octet-stream")
            
            }, progress: nil, success: { (_, response) -> Void in
                callBack(response: response, error: nil)
            }) { (_, error) -> Void in
                callBack(response: nil, error: error)
        }
    }
 
//    封装get/post请求
    func request(type: RequestType,url: String, params: AnyObject?, callBack: CallBack){
        
        if type == .GET {
            
            GET(url, parameters: params, progress: nil, success: { (_, response) -> Void in
                
                callBack(response: response, error: nil)
                
                }, failure: { (_, error) -> Void in
                    
                    callBack(response: nil, error: error)
            })
        }else{
            
            POST(url, parameters: params, progress: nil, success: { (_, response) -> Void in
                callBack(response: response, error: nil)
                }, failure: { (_, error) -> Void in
                callBack(response: nil, error: error)
            })            
        }
  
    }
}


// MARK:    -- 发送微博相关接口
extension LSZNetworkTools {
    
    func upload(access_token: String, status: String, image: UIImage, callBack: CallBack){
//        准备url地址
        let url = "https://upload.api.weibo.com/2/statuses/upload.json"
//        准备参数
        let params = [
            "access_token" : access_token,
            "status" : status
        ]
        
        let imageData = UIImageJPEGRepresentation(image, 0.5)!
        
        requestUpload(url, params: params, imageData: imageData, name: "pic", callBack: callBack)

    }
    
    
//    发送文字微博接口
    /* 
    - parameter access_token: 访问令牌
    - parameter status:       微博内容
    - parameter callBack:     数据回调
    */
    
    func update(access_token: String, status: String, callBack: CallBack){
        
        //  准备接口地址
        let url = "https://api.weibo.com/2/statuses/update.json"
//        准备参数
        let params = [
            "access_token" : access_token,
            "status" : status
        ]
//        使用共有的函数进行网络请求
        request(.POST, url: url, params: params, callBack: callBack)
        
        
    }
    
    
    
}

//拓展首页微博的相关接口
extension LSZNetworkTools {
    
    func loadStatus(maxId: Int64 = 0 , sinceId: Int64 = 0 ,callBack: CallBack){
//        接口地址
        let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
//        准备参数
        let params = [
            "access_token": LSZUserAccountViewModel.sharedUserAccount.accessToken!,
            "max_id": "\(maxId)",
            "since_id": "\(sinceId)"
        ]
        print(url + "?access_token=" + LSZUserAccountViewModel.sharedUserAccount.accessToken!)
        request(.GET, url: url, params: params, callBack: callBack)
    }
}


//拓展的OAuth登录相关接口
extension LSZNetworkTools{
     //  通过授权码(code)获取accesstoken
    func requestAccesstoken(code: String,callBack: CallBack){
//        准备借口地址
        let url = "https://api.weibo.com/oauth2/access_token"
        
//        准备参数
        let params = [
            "client_id": WeiboAppKey,
            "client_secret": WeoboAppSecret,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": WeiboRedirect_Url
        ]
//        使用通用借口请求
        request(.POST, url: url, params: params, callBack: callBack)
    }
    
//    获取用户信息
    func requestUserInfo(userAccount: LSZUserAccount, callBack: CallBack) {
        //  请求接口地址
        let url = "https://api.weibo.com/2/users/show.json"
        //  准备请求参数
        let params = [
            "access_token": userAccount.access_token!,
            "uid": "\(userAccount.uid)"
        ]
        
        request(.GET, url: url, params: params, callBack: callBack)
    }
    
}














